//
//  HomeController.swift
//  microblog
//
//  Created by cy on 2017/8/26.
//  Copyright © 2017年 Aisino. All rights reserved.
//

import UIKit

private let kCellID = "kCellID"

class HomeController: BaseController {
    
    // 模拟数据 数组
    fileprivate lazy var statusList = [String]()
    
    override func loadData() {
        print("开始加载数据")
        
        // 模拟延时加载数据
        DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + 1) { 
            
            for i in 0..<15 {
                if self.isPullup {
                    self.statusList.append("上拉第\(i)行")
                }
                else {
                    self.statusList.insert("第" + i.description + "行", at: 0)
                }
            }
            
            print("数据加载结束")
            self.refreshControl?.endRefreshing()
            self.isPullup = false
            self.tableView?.reloadData()
        }
    }
    
    @objc fileprivate func showFriends() {
        let test = TestController()
        navigationController?.pushViewController(test, animated: true)
    }
}


// MARK: - 表 数据源方法
extension HomeController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: kCellID, for: indexPath)
        cell.textLabel?.text = statusList[indexPath.row]
        
        return cell
    }
}


// MARK: - 设置界面
extension HomeController {
    
    override func setupTableView() {
        super.setupTableView()
        
        // 设置导航栏按钮
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", fontSize: 16, target: self, action: #selector(showFriends), isBack: false)
        // 注册cell
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: kCellID)
    }
}
